(function () {

    "use strict";

    /**
     * 'Unpaid','Pending','Dispatch','Complete', 'Refund'
     **/
    var api = {
        getListsUrl: '/api/order/orderLists',
        modifyStatus: '/api/order/modifyStatus',
        buyUrl: '/api/order/buyMore',
        confirmStatus: '/api/order/confirm'
    };
    sm.controller('wx-order-index-controller', ['$scope', '$http', 'Modal', 'Toast', function ($scope, $http, Modal, Toast){

        $scope.selectType = '';
        $scope.lists = [];
        $scope.loading = true;

        $scope.select = function (type) {

            if (type == $scope.selectType) {
                return false;
            }

            $scope.selectType = type;
            $scope.lists = [];
            $scope.loading = true;
            $http.get(api.getListsUrl, {
                params: {
                    orderType: $scope.selectType
                }
            }).success(function (res) {
                $scope.loading = false;
                if (res.state && res.data.total > 0) {
                    $scope.lists = res.data.data;
                }
            });

        };

        /**
         * 修改
         */
        $scope.chgStatus = function (index, type) {

            if (!$scope.lists[index]) {
                return false;
            }

            if ($scope.lists[index]['status'] == type) {
                return false;
            }

            var orderId = $scope.lists[index]['orderId'];
            var uuid = $scope.lists[index]['uuid'];
            var data = {
                'orderId': orderId,
                'uuid': uuid,
                'status': type
            };

            Modal.alert({
                title: '操作提示',
                content: '您确定要修改订单状态吗?',
                buttons: [{
                    name: '取消',
                    event: function () {}
                }, {
                    name: '确定修改',
                    event: function () {
                        doChangeStatus(data, index);
                    }
                }]
            });
        };

        function doChangeStatus(data, index) {
            Toast.loading();
            $http.post(api.modifyStatus, data).success(function (res) {
                Toast.stop();
                if (res.state) {
                    Toast.success({title: '修改成功'});
                    if (!$scope.selectType) {
                        if (data.status == 'Delete') {
                            $scope.lists.splice(index, 1);
                        } else {
                            $scope.lists[index]['status'] = 'Cancel';
                        }
                    } else {
                        $scope.lists.splice(index, 1);
                    }
                } else {
                    Toast.error('修改失败');
                }
            });
        }

        $scope.detail = function (l) {
            if (!l.orderId) {
                return false;
            }

            location.href="/order/detail?orderId=" + l.orderId
        };

        /**
         * 再来一单
         */
        $scope.buy = function (index) {

            var order = $scope.lists[index];
            if (!order.orderId) {
                return false;
            }

            Modal.alert({
                title: '操作提示',
                content: '您确定再次购买该产品吗?',
                buttons: [{
                    name: '取消',
                    event: function () {

                    }
                }, {
                    name: '确定购买',
                    event: function () {
                        doBuyMore(order.orderId);
                    }
                }]
            });
        };

        function doBuyMore(orderId) {
            Toast.loading({title: '请等待...'});
            $http.post(api.buyUrl, {
                orderId: orderId
            }).success(function (res) {
                if (res.state) {
                    location.href = '/order/confirm'
                } else {

                }
            });
        }

        $scope.confirm = function (index) {
            var order = $scope.lists[index];
            if (!order.orderId) {
                return false;
            }

            Modal.alert({
                title: '操作提示',
                content: '您确认收到相关货物吗?',
                buttons: [{
                    name: '取消',
                    event: function () {}
                }, {
                    name: '确认收货',
                    event: function () {
                        doConfirm(order.orderId);
                    }
                }]
            });
        };

        function doConfirm(orderId) {
            Toast.loading({title: '请等待...'});
            $http.post(api.confirmStatus, {
                orderId: orderId
            }).success(function (res) {
                Toast.stop();
                if (res.state) {
                    // 确认
                    location.href = '/order/success?orderId=' + orderId;
                } else {
                    Toast.alert({title: '确认失败'});
                }
            });
        }

        $scope.comment = function (index) {
            var order = $scope.lists[index];
            if (!order.orderId) {
                return false;
            }
            location.href = '/order/comment?orderId=' + order.orderId;
        };

        /**
         * 支付
         */
        $scope.pay = function (index) {
            var order = $scope.lists[index];
            if (!order.uuid) {
                return false;
            }
            location.href = '/order/pay?orderId=' + order.uuid;
        };

        $scope.refund = function (index) {
            var order = $scope.lists[index];
            if (!order.orderId) {
                return false;
            }
            location.href = '/order/refund?orderId=' + order.orderId;
        };

        $scope.checkRefund = function (index) {
            var order = $scope.lists[index];
            if (!order.orderId) {
                return false;
            }
            location.href = '/order/detail?orderId=' + order.orderId;
        };

        $scope.select();
    }]);
})();

